package me.winds.widget.usage;

import android.content.Context;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import me.winds.widget.R;


/**
 * Auther:  winds
 * Data:    2017/4/12
 * Desc:
 */

public class TitleView extends RelativeLayout {

    public TextView title_tv_left;
    public TextView title_tv_right;
    public TextView title_tv_center;
    public ImageView title_iv_left;
    public ImageView title_iv_right;


    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View view = View.inflate(context, R.layout.view_title, this);
        title_tv_left = findViewById(R.id.title_tv_left);
        title_tv_right = findViewById(R.id.title_tv_right);
        title_tv_center = findViewById(R.id.title_tv_center);
        title_iv_left = findViewById(R.id.title_iv_left);
        title_iv_right = findViewById(R.id.title_iv_right);
    }

    public TitleView setLeftImage(int resId) {
        if (!checkVisible(title_iv_left)) {
            title_iv_left.setVisibility(View.VISIBLE);
        }
        title_iv_left.setImageResource(resId);
        return this;
    }

    public TitleView setRightImage(int resId) {
        if (!checkVisible(title_iv_right)) {
            title_iv_right.setVisibility(View.VISIBLE);
        }
        title_iv_right.setImageResource(resId);
        return this;
    }


    public TitleView setText(TextView view, String text) {
        if (!checkVisible(view)) {
            view.setVisibility(View.VISIBLE);
        }
        view.setText(text);
        return this;
    }

    public TitleView setCenterText(CharSequence text) {
        title_tv_center.setText(text);
        return this;
    }

    public TitleView setCenterText(CharSequence text, int color) {
        title_tv_center.setText(text);
        title_tv_center.setTextColor(color);
        return this;
    }

    public TitleView setCenterText(int textId, int color) {
        title_tv_center.setText(textId);
        title_tv_center.setTextColor(color);
        return this;
    }



    public TitleView setCenterText(@StringRes int resId) {
        return setCenterText(getContext().getString(resId));
    }


    public TitleView setRightText(@StringRes int resId) {
        return setRightText(getContext().getString(resId));
    }

    public TitleView setRightText(CharSequence text) {
        title_tv_right.setVisibility(View.VISIBLE);
        title_tv_right.setText(text);
        title_tv_right.setVisibility(View.VISIBLE);
        return this;
    }

    public TitleView setRightText(CharSequence text, int color) {
        title_tv_right.setText(text);
        title_tv_right.setTextColor(color);
        title_tv_right.setVisibility(View.VISIBLE);
        return this;
    }


    boolean checkVisible(View view) {
        return view == null ? false : view.getVisibility() == VISIBLE ? true : false;
    }

    public TitleView setVisible(View view, boolean visible) {
        view.setVisibility(visible ? VISIBLE : GONE);
        return this;
    }


    public void setChildClickListener(View view, OnClickListener listener) {
        if (view != null && listener != null) {
            view.setOnClickListener(listener);
        }
    }

    public void setChildClickListener(int id, OnClickListener listener) {
        View view = findViewById(id);
        setChildClickListener(view, listener);
    }
}
